(function($){
    /**
     * 滚动到顶部.
     * @method scrollToTop
     */
    $.fn.scrollToTop = function(speed){
        $(this).stop().animate({
            scrollTop : 0
        }, speed || 600, "easeInOutQuint");      
    };
    
    /**
     * 滚动到底部.
     */
    $.fn.scrollToEnd = function(){
        var _this = this;
        window.setTimeout(function(){
            _this.scrollTop(_this.get(0).scrollHeight);
        }, 5);    
    };
    
    /**
     * 判断元素是否有滚动条.
     */
    $.fn.hasScrollbars = function() {
        var _this = $(this).get(0);
        // Move scroll bars
        _this.scrollLeft++;
        _this.scrollTop++;
        // Assume no scrollbars
        var d = 0;
        // Check for x,y
        if (_this.scrollLeft > 0 && _this.scrollTop > 0) 
            d = 3;
        // Check for x
        else if (_this.scrollLeft > 0)
            d = 1;
        // Check for y
        else if (_this.scrollTop > 0)
            d = 2;
        _this.scrollLeft--;
        _this.scrollTop--;
        return d;
    };
    
    //判断元素内容是否溢出.
    $.fn.isOverflow = function(errorSize){
        var ele = $(this).get(0);
        errorSize = errorSize || 0;
        var eleStyle = ele.style;
        if(!eleStyle.width || eleStyle.width === 'atuo'){
            return false;
        } else {
            return ( Math.abs( ele.scrollWidth - parseInt(eleStyle.width) ) > errorSize || 
                Math.abs( ele.scrollHeight - parseInt(eleStyle.height) ) > errorSize );
        }
    };
    
    /**
     * 同步滚动功能.
     * @param {Selector|JQuery} $secondElement
     * @param {String} type : 'H' or 'V', default : 'H'.
     * @param {Function} customSyncScrollFun : function($firstElement, $secondElement, scrollLeft, scrollTop)
     */
    $.fn.syncScrollWith = function($secondElement, type/* customSyncScrollFun */){
        var $firstElement = $(this), customSyncScrollFun;    
        
        if(!type){
            type = '';
        }
        
        if(Type.isFunction(type)){
            customSyncScrollFun = type;        
        } else {
            if(type.toLowerCase() === 'v'){
                customSyncScrollFun = function($firstElement, $secondElement, scrollLeft, scrollTop){
                    $secondElement.scrollTop(scrollTop);    
                };                        
            } else if(type.toLowerCase() === 'h' ){
                customSyncScrollFun = function($firstElement, $secondElement, scrollLeft, scrollTop){
                    $secondElement.scrollLeft(scrollLeft);
                };
            } else {            
                customSyncScrollFun = function($firstElement, $secondElement, scrollLeft, scrollTop){
                    $secondElement.scrollTop(scrollTop);                
                    $secondElement.scrollLeft(scrollLeft);
                };
            }
            
            //非用户自定义滚动连动逻辑，则需要反向同步滚动。
            $secondElement.scroll(function(e) {        
                customSyncScrollFun($secondElement, $firstElement, $secondElement.scrollLeft(), $secondElement.scrollTop());
            });
        }
        //滚动连动.
        $firstElement.scroll(function(e) {        
            customSyncScrollFun($firstElement, $secondElement, $firstElement.scrollLeft(), $firstElement.scrollTop());
        });   
        return this; 
    };
    
    /**
     * 获取滚动条的宽度.
     */
    $.getScrollBarWidth = function(){
        var scrollBarWidth, e1;      
        e1 = $('<div />').css({
            width : 100,
            height : 100,
            overflow : 'auto',
            position : 'absolute',
            top : -1000,
            left : 0
        }).prependTo('body').append('<div />').find('div').css({
            width : '100%',
            height : 200
        });
        scrollBarWidth = 100 - e1.width();  
        e1.parent().remove();   
        return scrollBarWidth;
    };
    
    //判断元素是否在可视区域.
    //isAllViewable : boolean. 元素是否整体都在可视区域.
    $.fn.onViewableArea = function(container, isAllViewable) {
        var ele = $(this).get(0);    
        container = container || document.body;
        var elePos = $(ele).getPosition(), 
            eleTop = elePos.top, 
            containerScrollTop = $(container).scrollTop(), 
            containerHeight = $(container).innerHeight(), 
            containerBottomTop = containerScrollTop + containerHeight;
    
        if (eleTop >= containerScrollTop && eleTop < containerBottomTop) {
            return true;
        }
        return false;
    };
    
    //获取滚动条信息.
    $.getScroll = function() {
        var top, left, width, height;
        if (document.documentElement && document.documentElement.scrollTop) {
            top = document.documentElement.scrollTop;
            left = document.documentElement.scrollLeft;
            width = document.documentElement.scrollWidth;
            height = document.documentElement.scrollHeight;
        } else if (document.body) {
            top = document.body.scrollTop;
            left = document.body.scrollLeft;
            width = document.body.scrollWidth;
            height = document.body.scrollHeight;
        }
        return {
            left : left,
            top : top,
            width : width,
            height : height
        };
    };
})(jQuery);